/**
 * Created by LinJe on 2015/10/22.
 */
;(function( $, window ) {

    $.fn.ugcs = function( settings ) {

        var $this = $(this),
            initLoadNum = settings.initLoad,
            isSingle = settings.singleUgc == true,
            productId = settings.productId,
            ugcId = settings.ugcId,
            oLoadMore = settings.oLoadMore,
            oTextTips = settings.oTextTips,
            pageStep = 1, totalSize = -1, type = 2;

        //默认加载
        (initLoadNum && initLoad()) || (isSingle && inserSingleUgc({'insertFunction':'html','ugcId':ugcId}));


        //事件委托 点击删除评论或微博
        $this.on('click', '.js-remove-ugc', function() {
            var userInfo = userServers.getUserInfo();

            if ( userInfo.userName ) {
                var thisDel = $(this),
                    oParentLi = thisDel.parents('li').eq(0);

                //打开删除提示窗
                window.globalLayer.openDelete({'callBack': function(canDel) {

                    if ( !canDel ) return;
                    //通知后端删除id为xxx的产品数据
                    commentServers.removeProdData({
                        'ugcId': oParentLi.attr('_ugcid'),
                        'ugcType': oParentLi.attr('_ugctype')
                    }, function( msg ) {
                        if ( !msg.error ) {
                            if ( msg.code == 200 ) {
                                //获取数据成功
                                oParentLi.remove();
                            } else {
                                window.globalLayer.openWarning({'autoClose':true, time: 2000, text:msg.description});
                            }
                        } else {
                            window.globalLayer.openWarning({'autoClose':true, time: 2000, text:msg.description});
                        }
                    });

                }});

            } else {
                window.globalLayer.openLogin(); //打开登陆提示弹窗
            }
        });


        //***********公用方法***************
        //初始化加载
        function initLoad( opts ) {
            pageStep = 1;
            totalSize = -1;
            /*if ( opts && opts.setView ) {
                opts.setView();
            }*/
            if ( oLoadMore ) oLoadMore.show().removeClass('mtb50').addClass('mtb300').html('<span class="loading"><a><i class="i-05"></i>正在加载更多</a></span>');
            if ( oTextTips ) oTextTips.hide();
            inserProductContent({
                'insertFunction':'html',
                'productId': productId,
                'ugcType': type,
                'pageStep': pageStep,
                'count': initLoadNum
            }, function( res ) {
                //pageStep++;
                if ( res.scrollEnd ) {
                    if ( oLoadMore ) oLoadMore.hide();
                    if ( oTextTips ) oTextTips.show();
                }
            });
        }


        //根据ugcId加载单条ugc数据
        function inserSingleUgc( opts, callBack ) {
            commentServers.getSingleUgc(opts, function( msg ) {
                if ( !msg.error ) {
                    if ( msg.code == 200 ) {
                        //获取数据成功
                        $this[opts.insertFunction]( getMoreHtml(msg.detail.ugcBeans) );
                        totalSize = msg.detail.totalSize;
                        callBack && callBack(true);
                    } else {
                        window.globalLayer.openWarning({'autoClose':true, time: 2000, text:msg.description});
                        callBack && callBack(false);
                    }
                } else {
                    window.globalLayer.openWarning({'autoClose':true, time: 2000, text:msg.description});
                }
            });
        }


        //插入商品内容
        function inserProductContent( opts, callBack ) {
            if ( totalSize != -1 && totalSize < pageStep ) {
                callBack && callBack({'scrollEnd':true});
                return;
            }
            commentServers.getProductData(opts, function( msg ) {
                if ( !msg.error ) {
                    if ( msg.code == 200 ) {
                        //获取数据成功
                        $this[opts.insertFunction]( getMoreHtml(msg.detail.ugcBeans) );
                        totalSize = msg.detail.totalSize;
                        callBack && callBack({'scrollEnd':totalSize<=1});
                    } else {
                        window.globalLayer.openWarning({'autoClose':true, time:2000, text:msg.description});
                        callBack && callBack();
                    }
                } else {
                    window.globalLayer.openWarning({'autoClose':true, time:2000, text:msg.description});
                }
            });
        }


        //获取多条商品的html
        function getMoreHtml( data ) {
            var totalHtml = '';

            for ( var i = 0, iLen = data.length; i < iLen; i ++ ) {
                totalHtml += getSingelHtml(data[i]);
            }
            return totalHtml;
        }

        function getVoteHtml( data ) {
            var sHtml = '';

            sHtml += '<ul class="vote">';
            sHtml +=    '<li class="good"><div class="con'+(function(){return data.chooseChannel==1?' current':''})()+'"><p class="num">'+ data.goodChannelNum +'票</p><p class="text">好渠道</p></div></li>';
            sHtml +=    '<li class="medium"><div class="con'+(function(){return data.chooseChannel==2?' current':''})()+'"><p class="num">'+ data.normalChannelNum +'票</p><p class="text">一般</p></div></li>';
            sHtml +=    '<li class="difference"><div class="con'+(function(){return data.chooseChannel==3?' current':''})()+'"><p class="num">'+ data.badChannelNum +'票</p><p class="text">假货/违规</p></div></li>';
            sHtml += '</ul>';

            return sHtml;
        }

        //获取单条商品的html
        function getSingelHtml( data ) {
            var sHtml = '';

            sHtml += '<li class="clearfix" _ugcid="'+ data.ugcId +'" _ugctype="'+ data.ugcType +'" " _replayUserId="'+ data.userId +'">';
            sHtml +=    '<div class="user-avatar"><a href="'+ data.userUrl +'"><img src="'+ data.userAvatar +'" width="50" height="50"></a></div>';
            sHtml += (function() {
                            //是否输出投票html
                            return data.ugcType == 4 ? getVoteHtml(data) : '';
                        })();
            sHtml +=    '<div class="goods-description-comment"> <i class="i-04"></i>';
            sHtml +=        '<div class="description">';
            sHtml +=            '<div class="text-3"><div class="con"><span class="user-name"><a href="'+ data.userUrl +'">'+ data.userName +'</a></span></div></div>';
            sHtml +=            (function() {
                //判断有无来源、购买渠道等url
                var str = '';
                if ( data.url ) {
                    str = '<div class="link"><span>'+ data.urlDescription +'</span><a href="'+ data.url +'" target="_blank">'+ data.url +'</a></div>';
                }
                return str;
            })();

            sHtml +=            (function() {
                //判断有无来源、购买渠道等url
                var str = '';
                if ( data.prodComment ) {
                    str ='<div class="text-2">'+ '<p>'+ data.prodComment +'</p>' +'</div>';
                }
                return str;
            })();

            sHtml +=            '<div class="w-opt"> <span class="time"><a href="'+ data.commentUrl +'">'+ data.time +'</a></span> '+ (function() {
                var str = '';

                if ( userServers.getUserInfo().userId == data.userId ) {
                    str = '<span class="js-remove-ugc"><a href="javascript:;">删除</a></span>';
                }
                return str;
            })() +' </div>';
            sHtml +=        '</div>';
            sHtml +=        '<div class="comment">';
            sHtml +=            '<div class="title"><span><em>'+ data.commentNum +'条评论</em><i class="i-03"></i></span></div>';
            sHtml +=        '</div>';
            sHtml +=        '<div class="bottom"></div>';
            sHtml +=    '</div>';
            sHtml += '</li>';


            return sHtml;
        }

        //对外公开的方法
        $.initLoad = initLoad;
        $.getMoreHtml = getMoreHtml;
        $.inserProductContent = inserProductContent;
        $.getInitLoadNum = function() {
            return initLoadNum;
        };
        $.getType = function() {
            return type;
        };
        $.setType = function( num ) {
            type = num;
        };
        $.scrollHandle = function( callBack ) {
            pageStep ++;
            $.inserProductContent({
                'insertFunction':'append',
                'productId': productId,
                'type': type,
                'pageStep': pageStep,
                'count': 20
            }, function( res ) {
                if ( res.scrollEnd ) {
                    callBack && callBack({'scrollEnd':true});
                } else {
                    $.canloadInfo = true;
                }
            });
        };


        return $this;

    }

})( jQuery, window );